﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;

namespace CuaHangMayTinh.PresentationLayer
{
    /// <summary>
    /// form hiển thị thêm xóa sửa tìm kiếm nhà cung cấp
    /// </summary>
    public partial class FormNhaCungCap : Form,IFormTV<NhaCungCapEntity>
    {
        /// <summary>
        /// chứa nội dung text lỗi
        /// </summary>
        private string Text = string.Empty;

        /// <summary>
        /// tables chua danh sach entity
        /// </summary>
        public DataTable Table { get; set; }
        /// <summary>
        /// danh sách nhà cung cấp
        /// </summary>
        private List<NhaCungCapEntity> nhaCungCapEntities; 

        /// <summary>
        /// phương thức khởi tạo
        /// </summary>
        public FormNhaCungCap()
        {
            InitializeComponent();
            nhaCungCapEntities = new List<NhaCungCapEntity>();
            LoadListView();
        }

        public FormNhaCungCap(bool[] so)
            : this()
        {
            if (so[0])
            {
                toolStripBtn_TimKiem.Enabled = true;
            }
            if (so[1])
            {
                toolStripBtn_Them.Enabled = true;
                themToolStripMenuItem.Enabled = true;
            }
            if (so[2])
            {
                toolStripBtn_Xoa.Enabled = true;
                xoaToolStripMenuItem.Enabled = true;
            }
            if (so[3])
            {
                toolStripBtn_Sua.Enabled = true;
                suaToolStripMenuItem.Enabled = true;
            }
        }

        /// <summary>
        /// load load thông tin của table lên datagirview 
        /// </summary>
        /// <param name="dt"></param>
        public void LoadListView(DataTable dt)
        {
            dataGridViewX_Select.DataSource = dt;
            if (dataGridViewX_Select.Columns.Count > 0)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        /// <summary>
        /// thêm click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_them_click(object sender, EventArgs e)
        {
            var nhacungcapThem = new FormNhaCungCapThem();
            if (nhacungcapThem.ShowDialog() == DialogResult.OK)
                LoadListView();
        }

        /// <summary>
        /// xóa click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_xoa_click(object sender, EventArgs e)
        {
            int i = 0;
            var c = MessageBox.Show("Bạn muốn xóa hay không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (c == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in dataGridViewX_Select.SelectedRows)
                {
                    if (NhaCungCapManager.Delete((int)row.Cells[0].Value,ref Text))
                    {
                        i++;
                    }
                    else
                    {
                        MessageBox.Show(Text);
                    }
                }
                MessageBox.Show("Xóa thành công " + i + " nhà cung cấp!");
                LoadListView();
            }
        }

        /// <summary>
        /// sửa click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_sua_click(object sender, EventArgs e)
        {
            if (dataGridViewX_Select.SelectedRows.Count > 0)
            {
                var row = dataGridViewX_Select.SelectedRows[0];
                var entity = new NhaCungCapEntity()
                {
                    MaNCC = (int) row.Cells[0].Value,
                    TenNCC = (string) row.Cells[1].Value,
                    MaSoThue = (string) row.Cells[2].Value,
                    DiaChi = (string) row.Cells[3].Value,
                    SoDT = (string) row.Cells[4].Value,
                    Email = (string) row.Cells[5].Value,
                    GhiChu = (string) row.Cells[6].Value,
                    TinhTrang = (bool) row.Cells[7].Value,

                };
                FormNhaCungCapThem nhacungcapThem = new FormNhaCungCapThem(entity);
                if (nhacungcapThem.ShowDialog() == DialogResult.OK)
                    LoadListView();
            }
        }

        /// <summary>
        /// load danh sach nhà cung cấp lên gridview
        /// </summary>
        public void LoadListView()
        {
            Table = DataUtil.ChangeColumn(NhaCungCapManager.Find_All());
            dataGridViewX_Select.DataSource = Table;
            if (dataGridViewX_Select.Columns.Count > 0)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        /// <summary>
        /// bắt sự kiện nút tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_timkiem_click(object sender, EventArgs e)
        {
            FormNhaCungCapTimKiem formNhaCungCapTimKiem = new FormNhaCungCapTimKiem(Table);
            formNhaCungCapTimKiem.ShowDialog();
        }

        /// <summary>
        /// sự kiện khi thay đổi text của textbox tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStriptxt_timnhanh_TextChanged(object sender, EventArgs e)
        {
            string text = (sender as ToolStripTextBox).Text;
            if (text != "")
            {
                DataTable dt = Table;

                var y = dt.AsEnumerable().Select(x => DataUtil.convertToUnSign3(x.Field<string>("Tên nhà cung cấp")).ToLower()).ToList();
                var z = y.Contains(text);
                var table2 =
                    dt.AsEnumerable()
                        .Where(
                            x =>
                                DataUtil.convertToUnSign3(x.Field<string>("Tên nhà cung cấp").ToLower())
                                    .StartsWith(DataUtil.convertToUnSign3(text.ToLower())));
                LoadListView(table2.Any() ? table2.CopyToDataTable() : null);
            }
            else
            {
                LoadListView(Table);
            }
        }

        /// <summary>
        /// thêm của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void themToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_them_click(sender, e);
        }

        /// <summary>
        /// xóa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_xoa_click(sender, e);
        }

        /// <summary>
        /// sửa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void suaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_sua_click(sender, e);
        }
    }
}
